package cn.tl.pet.service.impl;


import cn.tl.base.service.impl.BaseServiceImpl;
import cn.tl.org.domain.Shop;
import cn.tl.org.mapper.ShopMapper;
import cn.tl.pet.domain.SearchMasterMsg;
import cn.tl.pet.service.ISearchMasterMsgService;
import cn.tl.user.domain.User;
import cn.tl.utils.DistanceUtil;
import cn.tl.utils.LoginContext;
import cn.tl.utils.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;
    @Override
    public void publish(SearchMasterMsg masterMsg, HttpServletRequest request) {
        System.out.println(masterMsg);
        //待接单
        masterMsg.setState(0);
        //获取当前登录的用户
        User user = (User) LoginContext.getCurrentUser(request);
        masterMsg.setUser_id(user.getId());
        //离用户发布地址最近的商家
        List<Shop> shops = shopMapper.findAll();
        //最近的商家
        Point point = DistanceUtil.getPoint(masterMsg.getAddress());
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        masterMsg.setShop_id(shop.getId());
        //保存数据
        super.save(masterMsg);
    }
}
